//
// Created on 2025/11/4.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef PRE_EXPORT_BACKGROUND_CACHE_MANAGER_H
#define PRE_EXPORT_BACKGROUND_CACHE_MANAGER_H
#include <memory>
#include <mutex>
#include <thread>

#include "codec_common.h"
#include "codec_encoder.h"

#undef LOG_TAG
#define LOG_TAG "BackgroundCacheManager"

class BackgroundCache;

class BackgroundCacheManager {
public:
    static BackgroundCacheManager* GetInstance();
    ~BackgroundCacheManager();
    
    std::weak_ptr<BackgroundCache> RecodeBackgroundCache(const std::string &hashString,
        std::shared_ptr<BackgroundCache> backgroundCache);
    ResultCode start();
    ResultCode stop();
    ResultCode forceStop();

private:
    static std::mutex instanceMutex_;
    static BackgroundCacheManager* instance_;
    BackgroundCacheManager() = default;
    BackgroundCacheManager(const BackgroundCacheManager&) = delete;
    BackgroundCacheManager& operator=(const BackgroundCacheManager&) = delete;
    std::vector<std::weak_ptr<BackgroundCache>> GetBackgroundCacheList();
    
    void runTask();
    
    std::shared_ptr<CodecEncoder> encoder_ = std::make_shared<CodecEncoder>();
    std::atomic<bool> running_ = false;
    std::thread encoderThread_;
    CodecBufferQueue forwardQueue_;
    
    std::mutex listMutex_;
    std::vector<std::weak_ptr<BackgroundCache>> backgroundCacheList_;
    
};






#endif //PRE_EXPORT_BACKGROUND_CACHE_MANAGER_H
